<?php
class Category extends CI_Controller{
	
	public function __construct(){	
		parent::__construct();
		
		if($this->User_model->is_logged_in() != TRUE){
			$preUrl = $_SERVER['PATH_INFO'];
			$this->session->set_userdata('preUrl',$preUrl);
			redirect('login');
		}
		
		if($this->session->userdata('publish') == NULL || $this->session->userdata('controller') != 'category'){
			$this->session->set_userdata('publish',-1);
			$this->session->set_userdata('controller','category');
		}
	}
	
	public function index($pg = 1){
		$page = $pg;
		$rp = 2;
		$limit = 2;
		
		$publish = $this->session->userdata('publish');
		
		$total = $this->Category_model->getNumofCategory($publish);
			
		$data['categories'] = $this->Category_model->getCategoryByOffset(($page - 1) * $rp,$limit,$publish);
		$data['permissions'] = $this->Permission_model->getPermission($this->router->fetch_class());
		$data['pg'] = $this->paging($page, $rp, $total, $limit);
		$data['publish'] = $publish;
		if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == "XMLHttpRequest"){
			$this->load->view('category/index',$data);
		}
		else{
			$this->template->write_view('content','category/index',$data,TRUE);
			$this->template->load();
		}	
	}
	
	public function test($pg = 1){
		$page = $pg;
		$rp = 2;
		$limit = 2;
		
		$publish = $this->session->userdata('publish');
		
		$total = $this->Category_model->getNumofCategory($publish);
		print_r($this->Category_model->getCategoryByOffset(($page - 1) * $rp,$limit,$publish));
	}
	
	public function changeStatus(){
		
		try {
			$this->session->set_userdata('publish', $this->input->post('publish'));
			echo $this->session->userdata('publish');	
		} catch (Exception $e) {
			echo 'false';
		}
	}
	
	function paging($page,$rp,$total,$limit)
	{
        $limit -= 1;

        $mid = floor($limit/2);
        
        if ($total>$rp)
            $numpages = ceil($total/$rp);
        else
            $numpages = 1;
        
        if ($page>$numpages) $page = $numpages;

            $npage = $page;

        while (($npage-1)>0&&$npage>($page-$mid)&&($npage>0))
            $npage -= 1;
        
        $lastpage = $npage + $limit;
        
        if ($lastpage>$numpages) 
            {
            $npage = $numpages - $limit + 1;
            if ($npage<0) $npage = 1;
            $lastpage = $npage + $limit;
            if ($lastpage>$numpages) $lastpage = $numpages;
            }
        
        while (($lastpage-$npage)<$limit) $npage -= 1;        
        
        if ($npage<1) $npage = 1;
        
        //echo $npage; exit;
        
        $paging['first'] = 1;
        if ($page>1) $paging['prev'] = $page - 1; else $paging['prev'] = 1;
        $paging['start'] = $npage;
        $paging['end'] = $lastpage;
        $paging['page'] = $page;            
        if (($page+1)<$numpages) $paging['next'] = $page + 1; else $paging['next'] = $numpages;
        $paging['last'] = $numpages;
        $paging['total'] = $total;
        $paging['iend'] = $page * $rp;
        $paging['istart'] = ($page * $rp) - $rp + 1;
        
        if (($page * $rp)>$total) $paging['iend'] = $total;
        
        return $paging;    
	}
}